﻿using Engine;
using Engine.Audio;
using Engine.Graphics;
using Engine.Input;
using Engine.Media;
using Engine.Serialization;
using Game;
using GameEntitySystem;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime;
using System.Security.Claims;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Xml;
using System.Xml.Linq;
using TemplatesDatabase;
using XmlUtilities;
using static Game.SubsystemUpdate;
using static Game.TerrainContentsGenerator21;
using static System.Net.Mime.MediaTypeNames;
using Random = Game.Random;

namespace Game
{
    #region 成就系统
    public class 成就系统 : Component, IUpdateable, IDrawable // 
    {
        // 改为非静态，每个玩家实例有自己的成就数据
        public 成就数据Manager 玩家成就数据 = new 成就数据Manager();

        public ComponentPlayer m_componentPlayer;
        public ComponentInventory m_componentInventory;
        public BevelledButtonWidget 成就;

        public HashSet<int> 成就1Blockid = new HashSet<int>();//成就1的blockid
        public HashSet<int> 成就2Blockid = new HashSet<int>();//成就2的blockid
        public HashSet<int> 成就3Blockid = new HashSet<int>();//成就3的blockid

        public UpdateOrder UpdateOrder => UpdateOrder.Default;

        public float FloatUpdateOrder => default;

        public int[] DrawOrders => new int[] { 910 };

        public void Draw(Camera camera, int drawOrder)
        {

        }

        public void 初始化()
        {
            玩家成就数据.成就1 = false;
            玩家成就数据.成就2 = false;
            玩家成就数据.成就3 = false;
            玩家成就数据.成就4 = false;
            //写成就待加

            成就1Blockid = new HashSet<int>()
            {
                9,10,11,255
            };
            成就2Blockid = new HashSet<int>()
            {
                5
            };
            成就3Blockid = new HashSet<int>()
            {
                42
            };
            //写成就判断方块待加
        }

        public override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap)
        {
            初始化();
            m_componentPlayer = base.Entity.FindComponent<ComponentPlayer>();
            m_componentInventory = base.Entity.FindComponent<ComponentInventory>();

            玩家成就数据.成就1 = valuesDictionary.GetValue<bool>("成就1状态", false);
            玩家成就数据.成就2 = valuesDictionary.GetValue<bool>("成就2状态", false);
            玩家成就数据.成就3 = valuesDictionary.GetValue<bool>("成就3状态", false);
            玩家成就数据.成就4 = valuesDictionary.GetValue<bool>("成就4状态", false);
            //写成就待加 系统注册也加

            成就 = m_componentPlayer.GameWidget.Children.Find<BevelledButtonWidget>("chengjiouanniou");

            base.Load(valuesDictionary, idToEntityMap);
        }

        public override void Save(ValuesDictionary valuesDictionary, EntityToIdMap entityToIdMap)
        {
            valuesDictionary.SetValue("成就1状态", 玩家成就数据.成就1);
            valuesDictionary.SetValue("成就2状态", 玩家成就数据.成就2);
            valuesDictionary.SetValue("成就3状态", 玩家成就数据.成就3);
            valuesDictionary.SetValue("成就4状态", 玩家成就数据.成就4);
            //写成就待加 系统注册也加
        }

        public void 判断(int slotValue, int slotCount, HashSet<int> Blockid, int 成就id, int 需要的数量) //slotValue 玩家遍历背包的id slotCount 玩家遍历背包的数量 Blockid 达成成就对应的方块id 需要的数量 成就id(在下面case有定义)
        {
            if (slotCount >= 需要的数量 && (Blockid.Contains(slotValue))) //判断是否达成成就
            {
                switch (成就id)
                {
                    case 1:
                        玩家成就数据.成就1 = true;
                        m_componentPlayer.ComponentGui.DisplaySmallMessage("[想致富,先撸树]成就一以达成", Color.Green, true, true);
                        //奖励
                        m_componentPlayer.ComponentLevel.AddExperience(20, true);
                        break;
                    case 2:
                        玩家成就数据.成就2 = true;
                        m_componentPlayer.ComponentGui.DisplaySmallMessage("[石器时代]成就二以达成", Color.Green, true, true);
                        //奖励
                        m_componentPlayer.ComponentLevel.AddExperience(30, true);
                        break;
                    case 3:
                        玩家成就数据.成就3 = true;
                        m_componentPlayer.ComponentGui.DisplaySmallMessage("[开启导线时代]成就三以达成", Color.Green, true, true);
                        //奖励
                        m_componentPlayer.ComponentLevel.AddExperience(35, true);
                        break;
                    case 4:
                        玩家成就数据.成就4 = true;
                        m_componentPlayer.ComponentGui.DisplaySmallMessage("成就四以达成", Color.Green, true, true);
                        //奖励
                        m_componentPlayer.ComponentLevel.AddExperience(40, true);
                        break;
                    default:
                        m_componentPlayer.ComponentGui.DisplaySmallMessage("未知成就", Color.Red, true, true);
                        break;
                        //写成就待加
                }
            }
        }

        #region 成就1判断       

        public void 成就一达成条件()
        {
            if (玩家成就数据.成就1 == true) return;

            if (玩家成就数据.成就1 == false)
            {
                for (int i = 0; i < m_componentInventory.SlotsCount; i++)
                {
                    int slotValue = m_componentInventory.GetSlotValue(i);
                    int slotCount = m_componentInventory.GetSlotCount(i);
                    判断(slotValue, slotCount, 成就1Blockid, 1, 1);
                }
            }
            //int slotValue = m_componentPlayer.ComponentMiner.Inventory.GetSlotValue(m_componentPlayer.ComponentMiner.ActiveBlockValue);//获取当前激活方块的id
            //int slotCount = m_componentPlayer.ComponentMiner.Inventory.GetSlotCount(m_componentPlayer.ComponentMiner.ActiveBlockValue);//获取当前激活方块的数量           
        }

        #endregion 成就1判断end

        #region 成就2判断

        public void 成就二达成条件()
        {
            if (玩家成就数据.成就2 == true) return;

            for (int i = 0; i < m_componentInventory.SlotsCount; i++)
            {
                int slotValue = m_componentInventory.GetSlotValue(i);
                int slotCount = m_componentInventory.GetSlotCount(i);
                判断(slotValue, slotCount, 成就2Blockid, 2, 20);
            }
        }

        #endregion 成就2判断end

        #region 成就3判断
        public void 成就三达成条件()
        {
            if (玩家成就数据.成就3 == true) return;

            for (int i = 0; i < m_componentInventory.SlotsCount; i++) // 便利玩家背包 获取每个slot的id和数量
            {
                int slotValue = m_componentInventory.GetSlotValue(i);
                int slotCount = m_componentInventory.GetSlotCount(i);
                判断(slotValue, slotCount, 成就3Blockid, 3, 1);// 判断是否达成成就
            }
        }
        #endregion 成就3判断end

        public void Update(float dt) // 每帧调用
        {
            成就一达成条件();
            成就二达成条件();
            成就三达成条件();
            //写成就待加方法

            if (成就.IsClicked)
            {
                m_componentPlayer.ComponentGui.ModalPanelWidget = new 成就系统界面(m_componentPlayer, 玩家成就数据);
            }
        }
    }

    public class 成就系统界面 : CanvasWidget
    {
        public ComponentPlayer m_componentPlayer { get; set; }
        //写成就待加
        public BevelledButtonWidget 成就1;
        public BevelledButtonWidget 成就2;
        public BevelledButtonWidget 成就3;
        public BevelledButtonWidget 成就4;
        //写成就待加 按钮
        private 成就数据Manager m_成就数据;

        public 成就系统界面(ComponentPlayer componentPlayer, 成就数据Manager 成就数据)
        {
            m_componentPlayer = componentPlayer;
            m_成就数据 = 成就数据;

            try
            {
                XElement xElement = ContentManager.Get<XElement>("Widgets/成就界面", null);
                if (xElement != null)
                {
                    base.LoadContents(this, xElement);
                    this.成就1 = this.Children.Find<BevelledButtonWidget>("chengjiou1");
                    this.成就2 = this.Children.Find<BevelledButtonWidget>("chengjiou2");
                    this.成就3 = this.Children.Find<BevelledButtonWidget>("chengjiou3");
                    this.成就4 = this.Children.Find<BevelledButtonWidget>("chengjiou4");
                    //写成就待加

                    // 初始化时立即更新一次显示
                    更新成就显示();
                }
                else
                {
                    Log.Error("Failed to load 成就界面");
                }
            }
            catch (Exception ex)
            {
                Log.Error("Failed to load 成就界面: " + ex.Message);
            }
        }

        public void 点击行为()
        {
            if (成就1.IsClicked)
            {
                m_componentPlayer.ComponentGui.ModalPanelWidget = new 成就介绍界面(m_componentPlayer, "成就1");
            }
            if (成就2.IsClicked)
            {
                m_componentPlayer.ComponentGui.ModalPanelWidget = new 成就介绍界面(m_componentPlayer, "成就2");
            }
            if (成就3.IsClicked)
            {
                m_componentPlayer.ComponentGui.ModalPanelWidget = new 成就介绍界面(m_componentPlayer, "成就3");
            }
            if (成就4.IsClicked)
            {
                m_componentPlayer.ComponentGui.ModalPanelWidget = new 成就介绍界面(m_componentPlayer, "成就4");
            }
            //写成就待加
        }

        public void 更新成就显示()
        {
            if (成就1 != null)
            {
                成就1.Color = m_成就数据.成就1 ? Color.Green : Color.Red;
                成就1.Text = m_成就数据.成就1 ? "[想致富,先撸树]成就1:已完成" : "[想致富,先撸树]成就1:未完成";
            }

            if (成就2 != null)
            {
                成就2.Color = m_成就数据.成就2 ? Color.Green : Color.Red;
                成就2.Text = m_成就数据.成就2 ? "[石器时代]成就2:已完成" : "[石器时代]成就2:未完成";
            }
            if (成就3 != null)
            {
                成就3.Color = m_成就数据.成就3 ? Color.Green : Color.Red;
                成就3.Text = m_成就数据.成就3 ? "[开启导线时代]成就3:已完成" : "[开启导线时代]成就3:未完成";
            }
            if (成就4 != null)
            {
                成就4.Color = m_成就数据.成就4 ? Color.Green : Color.Red;
                成就4.Text = m_成就数据.成就4 ? "成就4:已完成" : "成就4:未完成";
            }
            //写成就待加
        }

        public override void Update()
        {
            // 每次更新都检查并更新显示
            更新成就显示();
            点击行为();
            base.Update();
        }
    }

    public class 成就数据Manager
    {
        public bool 成就1 = false;
        public bool 成就2 = false;
        public bool 成就3 = false;
        public bool 成就4 = false;
    }

    public class 成就介绍界面 : CanvasWidget
    {
        public ComponentPlayer m_componentPlayer { get; set; }

        public LabelWidget 字体;

        public 成就介绍界面(ComponentPlayer componentPlayer, string 成就名 = "成就1")
        {
            m_componentPlayer = componentPlayer;
            XElement xElement = ContentManager.Get<XElement>("Widgets/成就介绍界面", null);
            this.LoadContents(this, xElement);
            this.字体 = this.Children.Find<LabelWidget>("字体");
            this.字体.Text = XmlLoader.LoadXml("SelfData/成就", 成就名);
        }
    }

    #endregion 成就系统end


    public static class XmlLoader
    {

        public static string LoadXml(string xmlpath, string x)
        {
            try
            {

                string xml内容 = ContentManager.Get<string>(xmlpath, ".xml");
                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.LoadXml(xml内容);
                XmlNode node = xmlDocument.SelectSingleNode("成就");
                XmlNode node1 = node.SelectSingleNode(x);
                XmlElement 成就 = (XmlElement)node1;
                string 转制后的 = 成就.GetAttribute("Text");
                return 转制后的;
            }
            catch (Exception ex)
            {
                Log.Error("Failed to load xml: " + ex.Message);
                return ex.Message;
            }
        }

    }

}
